package com.evil.common.application.enums.button;

import com.evil.common.application.enums.robot.RobotTriggerTypeEnum;
import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;

@AllArgsConstructor
public enum TriggerActionEnum {
    /**
     * 触发动作
     */
    ADD_DATA(1, "添加数据", RobotTriggerTypeEnum.ADD),
    JUMP_LINK(2, "跳转链接", null),
    MODIFY_DATA(3, "修改数据", RobotTriggerTypeEnum.MODIFY),
    ;

    @Getter
    private final int id;
    @Getter
    private final String name;
    @Getter
    private final RobotTriggerTypeEnum robotTriggerTypeEnum;

    public static Optional<TriggerActionEnum> findById(Integer id) {
        if (null == id) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static TriggerActionEnum isExistById(Integer id) {
        return TriggerActionEnum.findById(id).orElseThrow(TriggerActionEnum::getBusinessException);
    }

    /**
     * 是否有匹配 规则
     *
     * @param id id
     * @return boolean
     */
    public static boolean hasRule(Integer id) {
        if (Objects.isNull(id)) {
            return false;
        }
        return ADD_DATA.id == id || MODIFY_DATA.id == id;
    }

    /**
     * 是否有筛选条件
     *
     * @param id id
     * @return boolean
     */
    public static boolean hasJudge(Integer id) {
        if (Objects.isNull(id)) {
            return false;
        }
        return MODIFY_DATA.id == id;
    }

    public static BusinessException getBusinessException() {
        return new BusinessException(RCodeEnum.ERROR_PARAMETER, "触发动作参数错误");
    }
}
